<?php
class BookCourseDao {
	public $table ='book_course';
	public function get($id){
		return ClassManager::getSql()->oneDataCache($this->table,'id',intval($id));
	}
	
	public function add($data){
		ClassManager::getSql()->insert($this->table,$data);
		return ClassManager::getSql()->addReturnId();
	}
	
	public function getBookCourseListByTeacherCLassidList($teacher_id_list){
	  $tmp = ClassManager::getSql()->dataList1($this->table,'*'," where  teacher_class_id in (".$teacher_id_list.") and class_status !=".CLASS_STATUS_3);
	  return $tmp;
	}
	
	public function getByBookNo($bookNo){
		return  ClassManager::getSql()->oneDataCache($this->table,'book_no', $bookNo);
	}
	
	public function getByUserId($userId){
		return ClassManager::getSql()->oneDataCache($this->table, 'user_id', $userId);
	}
	
	private  function searchWhere($where){
		if (!empty($where['start_book_date'])){
			$where['book_date'][] = array(strtotime($where['start_book_date']),'>=');
		}
		if (!empty($where['end_book_date'])){
			$where['book_date'][]= array(strtotime($where['end_book_date']),'<=');
		}
		$where1 = array();
		$likeWhere = array('book_no');
		foreach ($where as  $k=>$v){
			if (isset($v)&&$v!==""){
				if (in_array($k,$likeWhere) and !is_array($v)){
					$where1[$k] = array('%'.$v.'%' , 'like');
				}else{
					$where1[$k] = $v;
				}
			}
		}
		return $where1;
	}
	
	private  function searchMutilWhere($where){
     	$where1 = '';
		if(!empty($where['start_book_date'])){
			$where1 .=" and a.book_date >=".strtotime($where['start_book_date']);
		}
		if(!empty($where['end_book_date'])){
			$end_time = strtotime($where['end_book_date'])+86399;
			$where1 .=" and a.book_date <=".$end_time;
		}
		
		if(!empty($where['key_word'])){
			$where1 .=" and (a.book_no like '%".$where['key_word']."%'";
			$where1 .=" or a.english_name like '%".$where['key_word']."%' ";
			$where1 .=" or a.qq like '%".$where['key_word']."%'  ";
			$where1 .=" or a.phone like '%".$where['key_word']."%'  ";
			$where1 .=" or t.serial_number like '%".$where['key_word']."%'  ";
			$where1 .=" or t.name like '%".$where['key_word']."%') ";
		}
		
		if(isset($where['user_id'])&&$where['user_id']!=""){
			$where1 .=" and a.user_id ='".$where['user_id']."'";
		}
		
		if(isset($where['teacher_id'])&&$where['teacher_id']!=""){
			$where1 .=" and a.teacher_id ='".$where['teacher_id']."'";
		}
		
		if(isset($where['book_type'])&&$where['book_type']!=""){
			$where1 .=" and a.book_type ='".$where['book_type']."'";
		}
		
		if(isset($where['class_status'])&&$where['class_status']!=""){
			$where1 .=" and a.class_status ='".$where['class_status']."'";
		}
		
		if(isset($where['class_status_teacher'])&&$where['class_status_teacher']){
			$where1 .=" and a.class_status !='".CLASS_STATUS_3."'";
		}
		
		if(isset($where['teacher_status'])&&$where['teacher_status']!=""){
			$where1 .=" and a.teacher_status ='".$where['teacher_status']."'";
		}
		
		if(isset($where['book_time'])&&$where['book_time']!=""){
			$where1 .=" and a.book_time ='".$where['book_time']."' ";
		}
	
		return $where1;
	}
	public function searchData($where=array(),$order=''){
		$where = $this->searchWhere($where);
		return ClassManager::getSql()->onedata1($this->table,'*',$where);
	}
	
	public function searchMutilList($where=array(),$order='',$limit=0){
		$where = $this->searchMutilWhere($where);
		$sql  = " select a.* from eo_book_course a "; 
		$sql .= " left join eo_teacher t on a.teacher_id=t.id left join eo_user u  on a.user_id=u.id ";
		$sql .= " where 1=1 ";
		$sql .= $where ;
		$sql .= " ";
		$sql .= $order;
		//var_dump($sql);
		return ClassManager::getSql()->dataMutillist('bookcourse and teacher and user ',$sql,$limit);
	}
	public function searchMutilNumber($where=array()){
		$where = $this->searchMutilWhere($where);
		$sql  = " select count(1) cnt from eo_book_course a "; 
		$sql .= " left join eo_teacher t on a.teacher_id=t.id left join eo_user u  on a.user_id=u.id ";
		$sql .= " where 1=1  ";
		$sql.=  $where;
		$numberList = ClassManager::getSql()->dataMutillist('bookcourse and teacher and user number ',$sql);
		if(!empty($numberList)){
			return $numberList[0][0];
		}
		return 0;
	}
	
	public function searchList($where=array(),$order='',$limit=0){
		$where = $this->searchWhere($where);
		return ClassManager::getSql()->dataList1($this->table,'*',$where,$order,$limit);
	}
	
	public function searchNumber($where){
		$where = $this->searchWhere($where);
		$tmp = ClassManager::getSql()->onedata1($this->table,"count(*)  ",$where);
		return $tmp[0];
	}
	public function update($data,$id){
		ClassManager::getSql()->updates($this->table, $data,"where id=".intval($id));
	}

	public function updateByIdList($data,$id_list_str){
		ClassManager::getSql()->updates($this->table, $data,"where in (".$id_list_str." ) ");
	}
}

?>